<?php
    class Product_Model extends CI_Model
    {
		var $table = "db_product";
        public function __construct()
        {
            parent::__construct();
        }
		public function add_product($data)
		{
			$data = array(
				'name_product' => $data['name_product'],
				'description'  => $data['description'],
				'meta_keyword' => $data['meta_keyword'],
				'meta_description' => $data['meta_description'],
				'url'			=> $data['url'],
				'price'			=> $data['price'],
				'ms'			=> $data['ms'],
				'weight'		=> $data['weight'],
				'height'		=> $data['height'],
				'soluong'		=> $data['soluong'],
				'sale_off'		=> $data['sale_off'],
				'time_sale_off'	=> $data['time_sale_off'],
				'time_beign'	=> $data['time_begin'],
				'time_finish'	=> $data['time_finish'],
				'image'			=> $data['image'],
				'image_thumb'	=> $data['image_thumb'],
				'active'		=> $data['active'],
				'order_product' => $data['order_product'],
				'vitri_show'	=> $data['vitri_show'],
                'id_cats'       => $data['id_cats'],
                'vitri_left'    => $data['vitri_left'],
                'vitri_right'    => $data['vitri_right'],
                'slidershow'    => $data['slidershow']					
			);
			$this->db->set($data);
			if($this->db->insert($this->table)) return true; else return false;
		}
		public function update_product($id,$data)
		{
			$this->db->where('id_product',$id);
			$data = array(
				'name_product' => $data['name_product'],
				'description'  => $data['description'],
				'meta_keyword' => $data['meta_keyword'],
				'meta_description' => $data['meta_description'],
				'url'			=> $data['url'],
				'price'			=> $data['price'],
				'ms'			=> $data['ms'],
				'weight'		=> $data['weight'],
				'height'		=> $data['height'],
				'soluong'		=> $data['soluong'],
				'sale_off'		=> $data['sale_off'],
				'time_sale_off'	=> $data['time_sale_off'],
				'time_beign'	=> $data['time_begin'],
				'time_finish'	=> $data['time_finish'],
				'image'			=> $data['image'],
				'image_thumb'	=> $data['image_thumb'],
				'active'		=> $data['active'],
				'order_product' => $data['order_product'],
				'vitri_show'	=> $data['vitri_show'],
                'id_cats'       => $data['id_cats'],
                'vitri_left'    => $data['vitri_left'],
                'vitri_right'    => $data['vitri_right'],
                'slidershow'    => $data['slidershow']				
			);
			$this->db->set($data);
			if($this->db->update($this->table)) return true; else return false;
		}
		public function list_product()
		{
			return $this->db->get($this->table)->result();
		}
        function change_update_active($id,$data)
        {
            $this->db->where('id_product',$id);
            $data = array(
            'active' => $data['active']
            );
            $this->db->set($data);
            if($this->db->update($this->table)) return true; else return false;
        }
		public function get_id_product($id)
		{
			$this->db->where('id_product',$id);
			return $this->db->get($this->table)->row();
		}
         function total($dk = null)
		{
			if($dk != null)
			{
				$this->db->where('id_product', $dk);
			}
			
			
			
			return $this->db->count_all_results($this->table);
		}
        function product_phantrang($begin,$max)
		{
			if($begin == ''){
				$begin = 0;
			}
			if($max >= 0 && $max != ''){
				$limit = 'limit '.$begin.','. $max;
			}else{
				$limit ='';	
			}
            
		return $this->db->query('select * from db_product  order by id_product '. $limit)->result();	
		}
        function get_name_product($id)
        {
            $q = $this->db->get_where($this->table,array('id_product'=>$id));
            if($q->num_rows() > 0)
            {
                return $q->row()->name_product;
            }
            else
            {
                return false;
            }
        }
        function delete_product($id)
        {
            $this->db->where('id_product',$id);
            return $this->db->delete($this->table);
        }
    }
?>